//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.DataLayer
{
    using System.Linq;
    using System;    
    using System.Collections.Generic;

    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ActivitySecurity : IDataLayerSecurityFilter
    {
        private Connection connection;
        private ConnectionSecure secureConnection;
        private ActivityActionCalls actions;
        private ActivityBeforeCalls before;
        private ActivityAfterCalls after;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivitySecurity"/> class.
        /// </summary>
        /// <param name="secureConnection">The secure connection.</param>
        /// <param name="directConnection">The direct connection.</param>
        public ActivitySecurity(ConnectionSecure secureConnection, Connection directConnection)
        {
            this.connection = directConnection;
            this.secureConnection = secureConnection;

            this.actions = new ActivityActionCalls(connection);
            this.before = new ActivityBeforeCalls(connection);
            this.after = new ActivityAfterCalls(connection);
        }

        #region IDataLayerSecurityFilter Members

        /// <summary>
        /// Gets the actions.
        /// </summary>
        /// <value>The actions.</value>
        public IDataLayerSecurityActionCalls Actions
        {
            get { return this.actions; }
        }

        /// <summary>
        /// Gets the before.
        /// </summary>
        /// <value>The before.</value>
        public IDataLayerSecurityBeforeCalls Before
        {
            get { return this.before; }
        }

        /// <summary>
        /// Gets the after.
        /// </summary>
        /// <value>The after.</value>
        public IDataLayerSecurityAfterCalls After
        {
            get { return this.after; }
        }

        #endregion

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns></returns>
        public static User GetUser(Connection connection)
        {
            if (ConnectionSecure.AuthenticationToken.Identity.IsAuthenticated)
            {
                UserCredential.ISearch searchQuery = UserCredential.ISearch.Create();
                searchQuery.Query = UserCredential.ISearch.Credential(
                    StringField.Condition.Equals,
                    ConnectionSecure.AuthenticationToken.Identity.Name);

                UserCredential credential = UserCredential.Search(searchQuery, connection).Where(cred => !cred.User.IsDeleted && cred.User.Enabled).FirstOrDefault();

                if (credential != null)
                {
                    return credential.User;
                }                
            }
            throw new NotAuthenticatedException("User not Authenticated");
        }
    }
}
